home *** CD-ROM | disk | FTP | other *** search
- UNIT arith_h;
-
- { ------------------------------------------------------------------
-
- This program and its associates implement in Turbo Pascal v5
- the aritmetic encoding/decoding algorithms presented in the papers
-
- "Arithmetic Coding for Data Compression"
-
- by Ian H. Witten
- Radford M. Neal
- John G. Cleary
-
- pp 520 - 540 of June 1987 Communications of the ACM
-
- and
-
- "An Adaptive Dependency Source Model For Data Compression"
-
- by David M. Abrahamson
-
- pp 77 - 83 of January 1989 Communications of the ACM
-
- ------------------------------------------------------------------
-
- Implemented by Ken Westerback : CompuServe 73547,3520
-
- version 1.0 released 89/02/19
- version 2.0 released 89/02/27
-
- These programs, units and associated documentation are released
- into the public domain to be used and abused as your whims
- dictate.
-
- Feel free to distribute/incorporate/improve as desired.
-
- >>>>> Use at your own risk! <<<<<
-
- Comments and suggestions welcome via CompuServe.
-
- ------------------------------------------------------------------
- }
-
-
- INTERFACE uses dos;
-
- type code_value = word; { type of an arithmetic code value }
- bit_buffer = array [ 0..511 ] of longint; { 1 allocation unit of 2048 bytes }
-
-
- const code_value_bits = SizeOf ( code_value ) * 8; { number of bits in a code value }
-
- top_value = (1 shl code_value_bits) - 1; { largest code value }
-
- first_qtr = (top_value div 4) + 1; { point after first quarter }
- half = 2 * first_qtr; { point after first half }
- third_qtr = 3 * first_qtr; { point after third quarter }
-
- bits_per_buffer = 32;
-
- high_bit = 1 shl ( bits_per_buffer - 1 );
-
- one_masks : array [ 1..32 ] of longint =
- ( $80000000, $C0000000, $E0000000, $F0000000
- ,$F8000000, $FC000000, $FE000000, $FF000000
- ,$FF800000, $FFC00000, $FFE00000, $FFF00000
- ,$FFF80000, $FFFC0000, $FFFE0000, $FFFF0000
- ,$FFFF8000, $FFFFC000, $FFFFE000, $FFFFF000
- ,$FFFFF800, $FFFFFC00, $FFFFFE00, $FFFFFF00
- ,$FFFFFF80, $FFFFFFC0, $FFFFFFE0, $FFFFFFF0
- ,$FFFFFFF8, $FFFFFFFC, $FFFFFFFE, $FFFFFFFF
- );
-
- var bits_sent : longint; { counts of bits/chars for use in displays }
- bits_gotten : longint; { and calculations }
-
- buffer : longint; { where the bits are written to or read from }
- big_buffer : bit_buffer;
- bits_file : file;
-
- buffer_index : longint; { index into bit_buffer of word in buffer }
-
- bits_to_go : 0..32; { number of bits still waiting in buffer }
-
- sending_crap : boolean; { true if input exhausted & random bits sent }
-
- bits_to_follow : longint; { # of opposite bits to send after next bit }
-
- value : code_value; { currently seen code value }
- low : code_value; { low end of current value range }
- high : code_value; { high " " " " " }
-
-
- IMPLEMENTATION
-
- BEGIN
-
- { initialize our variables to safe values, cuz others are untrustworthy! }
-
- bits_sent := 0;
- bits_gotten := 0;
- buffer := 0;
- buffer_index := 0;
- bits_to_follow := 0;
- bits_to_go := 0;
-
- { start with largest possible range and no value }
-
- value := 0;
-
- low := 0;
- high := top_value;
-
- sending_crap := false; { assume the best }
-
- fillchar ( big_buffer, sizeof(big_buffer), 0 );
-
- END. { arithmetic coding header }